package com.xxx.video.dynamic.fragments;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.dynamic.activitys.PersonInfoActivity;
import com.xxx.video.mine.adapters.SearchFriendResultListAdapter;
import com.xxx.video.mine.beans.SearchResultListBean;
import com.xxx.video.mine.presenters.FindFriendResultListPresenter;
import com.xxx.video.mine.presenters.IFindFriendResultListView;

import java.util.Objects;

import butterknife.BindView;

public class FindFriendResultListFragment extends BaseFragment<FindFriendResultListPresenter>
        implements IFindFriendResultListView, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener,
        BaseRecycleViewAdapter.OnItemClickListener<SearchResultListBean.ListBean> {
    @BindView(R.id.rcv_find_friend_result_list)
    RefreshRecyclerView rcvFindFriendResultList;
    /**
     * this is search Content
     */
    private String searchContent;
    /**
     * page
     */
    private int page = 1;
    private SearchFriendResultListAdapter mSearchFriendResultListAdapter;

    public static FindFriendResultListFragment newInstance(Bundle args) {
        FindFriendResultListFragment fragment = new FindFriendResultListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected FindFriendResultListPresenter createPresenter() {
        return new FindFriendResultListPresenter(Objects.requireNonNull(getActivity()), this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_find_friend_result_list;
    }

    @Override
    public void initData(Bundle extras) {
        initList();
        getPresenter().search(searchContent, page);
    }

    @Override
    public void refreshData(Bundle extras) {

    }

    public void setSearchKeyWords(String searchContent) {
        page = 1;
        if (rcvFindFriendResultList != null) {
            rcvFindFriendResultList.setLoadMoreEnabled(true);
        }
        this.searchContent = searchContent;
        if (getPresenter() != null) {
            getPresenter().search(searchContent, page);
        }
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initList() {
        page = 1;
        rcvFindFriendResultList.setPullRefreshEnabled(false);
        rcvFindFriendResultList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rcvFindFriendResultList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
        mSearchFriendResultListAdapter = new SearchFriendResultListAdapter(Objects.requireNonNull(getActivity()));
        mSearchFriendResultListAdapter.setOnItemClickListener(this);
        rcvFindFriendResultList.setAdapter(mSearchFriendResultListAdapter);
    }

    @Override
    public void initFriendList(SearchResultListBean searchResultListBean) {
        rcvFindFriendResultList.resetState();
        showLayoutStatus(1);
        if (mSearchFriendResultListAdapter != null) {
            if (page == 1) {
                mSearchFriendResultListAdapter.setData(searchResultListBean.list);
                if (ListUtils.isEmpty(searchResultListBean.list)) {
                    showLayoutStatus(2);
                }
            } else {
                mSearchFriendResultListAdapter.addData(searchResultListBean.list);
            }
        }
    }

    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        getPresenter().search(searchContent, page);
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().search(searchContent, page);
    }

    @Override
    public void onItemClick(SearchResultListBean.ListBean listBean, int position, int type) {
        if (type == 1) {
            getPresenter().addFriends(listBean, position);
        } else {
            startPersonInfoActivity(listBean.userId);
        }
    }

    @Override
    public void addFriendSuccess(int position) {
        if (mSearchFriendResultListAdapter != null) {
            mSearchFriendResultListAdapter.addFriendSuccess(position);
        }
    }


    @Override
    public void startPersonInfoActivity(String userId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", userId);
        intent2Activity(PersonInfoActivity.class, bundle);
    }

}
